<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    .main{
      margin: 10vh auto 0;
      width: 400px;
      height: 200px;
      position: relative;
    }
    .uls{
      margin: 0 auto;
      width: 400px;
      height: 200px;
      position: relative;
      overflow: hidden;
    }
    .ul {
      display: flex;
      list-style: none;
      position: absolute;
      left: 0;
      top: 0;
    }
    .ul li{
      
    }
    img{
      width: 400px;
      height: 200px;
    }

    /* 左右箭头 */
    .btn{
      margin: 0;
      padding: 0;
      display: none;
    }
    .btn span{
      position: absolute;
      width: 40px;
      height: 40px;
      top: 50%;
      margin-top: -20px;
      line-height: 40px;
      text-align: center;
      font-weight: bold;
      font-family: Simsun;
      font-size: 30px;
      border: 1px solid #fff;
      opacity: 0.3;
      cursor: pointer;
      color: #fff;
      background: rgb(150, 146, 146);
    }
    #left{
      left: 5px;
    }
    #right{
      left: 100%;
      margin-left: -45px;
    }
    /* 圆点 */
    .litCir{
      position: absolute;
      margin: 0;
      padding: 0;
      right: 10px;
      bottom: 10px;
      list-style: none;
      display: flex;
    }
    .litCir li{
      width: 10px;
      height: 10px;
      background: rgba(0, 0, 0, 0.5);
      margin: 0 5px;
      cursor: pointer;
    }
    .active{
      background-color: rgb(255, 255, 255) !important;
    }
  </style>
</head>
<body>
  <div class="main">
    <div class="uls">
      <ul class="ul">
        <li><img src="./img/1.jpg"></li>
        <li><img src="./img/2.jpg"></li>
        <li><img src="./img/3.jpg"></li>
        <li><img src="./img/4.jpg"></li>
      </ul>
      <ul class='litCir'>
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
    <div class="btn">
      <span id="left">&lt;</span>
      <span id="right">&gt;</span>
    </div>
  </div>
  <script>
    let Oul = document.getElementsByClassName('ul')[0]
    let OlitCir = document.getElementsByClassName('litCir')[0].children

    let indeximg = 0  //当前图片
    let btnindex = 0 //当前圆点

    //left偏移量 设置函数
    function rollFn(value){
      Oul.style.left = value +'px'
    }

    //setInterval 自动调用
    function autoRun(){
      indeximg++
      btnindex++
      if(indeximg>=4){  //判断是否到最后一张
        indeximg = 0
        btnindex = 0
        
      }
      rollFn(-indeximg*400)


      if(btnindex>=4){
        btnindex = 0
      }
      for(var i=0; i<4; i++){
        OlitCir[i].className = ""
      }
      OlitCir[btnindex].className = "active"
    }
    setInterval(autoRun,2000)

    //鼠标移入移出 显示左右切换箭头
    let Omain = document.getElementsByClassName('main')[0]
    let Obtn = document.getElementsByClassName('btn')[0]
    Omain.onmouseover = function(){
      Obtn.style.display = 'block'
    }
    Omain.onmouseout = function(){
      Obtn.style.display = 'none'
    }

    //左右箭头切换
    let Oleft = document.getElementById('left')
    let Oright = document.getElementById('right')
    Oleft.onclick = function(){
      indeximg--
      btnindex--
      if(indeximg==-1){
        indeximg = 3
        btnindex = 3
      }
      if(indeximg>=4){  //判断是否到最后一张
        indeximg = 0
      }
      
      rollFn(-indeximg*400)
      if(btnindex>=4){
        btnindex = 0
      }
      for(var i=0; i<4; i++){
        OlitCir[i].className = ""
      }
      OlitCir[btnindex].className = "active"
    }
    Oright.onclick = autoRun

    //给圆点添加切换事件
    for(let i=0; i<4; i++){
      OlitCir[i].onclick = function(){
        indeximg = i
        btnindex = i
        rollFn(-indeximg*400)
        for(var j=0; j<4; j++){
          OlitCir[j].className = ""
        }
        OlitCir[btnindex].className = "active"
        }
    }
  </script>
</body>
</html>